#include "Graphics/OpenGL/GLDeviceVertexBuffer.h"

#include "Framework/StableHeaders.h"
#include "Graphics/IDeviceVertexBuffer.h"
#include "Graphics/OpenGL/GLPrerequisites.h"


namespace tyro
{
	namespace Graphics
	{


		GLDeviceVertexBuffer::GLDeviceVertexBuffer( IDeviceBufferManagerBase* mgr, size_t vertexSize, size_t numVertices )
			: IDeviceVertexBuffer(mgr, vertexSize, numVertices)
		{
				glGenBuffersARB(1, &bufferID);
			
				glBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferID);

				glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeInBytes, NULL, GL_STATIC_DRAW_ARB);
		}

		void GLDeviceVertexBuffer::WriteData( size_t memOffset, size_t length, const void* source )
		{
			glBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferID);

			if (memOffset == 0 && length == sizeInBytes)
			{
				glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeInBytes, source, GL_STATIC_DRAW_ARB);
			}
			
		}

		void GLDeviceVertexBuffer::ReadData( size_t memOffset, size_t length, void* destination )
		{
			glBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferID);

			glGetBufferSubDataARB(GL_ARRAY_BUFFER_ARB, memOffset, length, destination);
		}

		void* GLDeviceVertexBuffer::LockImpl( size_t offset, size_t length )
		{
			void* retrn = 0;

			//glBindBufferARB( GL_ARRAY_BUFFER_ARB, bufferID );

			

			
			return retrn;
		}

		void GLDeviceVertexBuffer::UnlockImpl( void )
		{
			glBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferID);
		}


	}
}